module.exports = {
	/**
	 * 添加单条数据
	 * @url admin/recharge/sys/add 前端调用的url参数地址
	 * data 请求参数 说明
	 * res 返回参数说明
	 * @params {Number} code 错误码，0表示成功
	 * @params {String} msg 详细信息
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
			tvinfo,
			recharge_amount,
			bean,
			recharge_text,
			subject,
			days,
			hour,
			judge,
			jiaobiao,
			invite_code,
			appid,
			appname,
			sort,
			give_bean, //多送多少豆
			percentage_user, //xx%的用户选择
			indulgence, //是否特惠充值
			tv_id,
			_add_time,
			_add_time_str
		} = data;
		let res = {
			code: 0,
			msg: 'ok'
		};
		// 业务逻辑开始-----------------------------------------------------------
		// 获取前端传过来的参数

		// 获取前端传过来的参数
		try {
			recharge_amount = tvinfo.recharge_amount,
				bean = tvinfo.bean,
				recharge_text = tvinfo.recharge_text,
				subject = tvinfo.subject,
				days = tvinfo.days,
				hour = tvinfo.hour,
				judge = tvinfo.judge,
				jiaobiao = tvinfo.jiaobiao,
				invite_code = tvinfo.invite_code,
				appid = tvinfo.appid,
				appname = tvinfo.appname,
				sort = tvinfo.sort,
				give_bean = tvinfo.give_bean, //多送多少豆
				percentage_user = tvinfo.percentage_user, //xx%的用户选择
				indulgence = tvinfo.indulgence, //是否特惠充值
				_add_time = tvinfo._add_time,
				_add_time_str = tvinfo._add_time_str,
				tv_id = tvinfo.tv_id
		} catch (err) {

		}


		let dbName = "b_recharge"; // 表名
		for (let i = 0; i < userInfo.role.length; i++) {
			//只有权限里包含admin或agent时，才允许加充值方案
			if (userInfo.role[i] == 'agent' || userInfo.role[i] == 'admin') {
				// 执行 数据库add 命令
				res.id = await vk.baseDao.add({
					dbName,
					dataJson: {
						nickname: userInfo.nickname,
						username: userInfo.username,
						user_id: userInfo._id,
						middleman_id: userInfo.middleman_id || 0,
						middleman_username: userInfo.middleman_username || 0,
						middleman_nickname: userInfo.middleman_nickname || 0,
						recharge_amount,
						bean,
						jiaobiao,
						recharge_text: recharge_text || "",
						subject,
						days: days || 0,
						hour: hour || 0,
						judge,
						invite_code,
						give_bean, //多送多少豆
						percentage_user, //xx%的用户选择
						indulgence, //是否特惠充值
						appid,
						appname,
						tv_id,
						sort,
						_add_time,
						_add_time_str
					}
				});
				return res;
			}
		}
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}